package chaowan.Mapper;

import chaowan.pojo.OrderDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderDetailMapper {

    @Select("SELECT * FROM order_detail")
    List<OrderDetail> selectAll();

    @Select("SELECT * FROM order_detail WHERE detail_id = #{detailId}")
    OrderDetail selectById(Long detailId);

    @Insert("INSERT INTO order_detail(order_id, goods_id, qty, price) " +
            "VALUES(#{orderId}, #{goodsId}, #{qty}, #{price})")
    @Options(useGeneratedKeys = true, keyProperty = "detailId")
    int insert(OrderDetail orderDetail);

    @Update("UPDATE order_detail SET order_id=#{orderId}, goods_id=#{goodsId}, qty=#{qty}, price=#{price} WHERE detail_id=#{detailId}")
    int update(OrderDetail orderDetail);

    @Delete("DELETE FROM order_detail WHERE detail_id = #{detailId}")
    int deleteById(Long detailId);

    @Select("SELECT * FROM order_detail WHERE order_id = #{orderId}")
    List<OrderDetail> findByOrderId(Long orderId);

    @Select("SELECT * FROM order_detail WHERE order_id = #{orderId} LIMIT #{offset}, #{size}")
    List<OrderDetail> selectByOrderIdWithPagination(@Param("orderId") Long orderId, @Param("offset") int offset, @Param("size") int size);

    @Select("SELECT COUNT(*) FROM order_detail WHERE order_id = #{orderId}")
    int countByOrderId(@Param("orderId") Long orderId);

    @Delete("DELETE FROM order_detail WHERE order_id = #{orderId}")
    int deleteByOrderId(Long orderId);
}

